home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / demos / multiwin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.8 KB  |  308 lines

  1. /* Copyright (c) Silicon Graphics, Inc. 1996 */
  2.  
  3. /* multiwin.c - Demonstrates the use of multiple windows.
  4.  *        Use the mouse's position in the window to
  5.  *        determine the location of the eye position.
  6.  *
  7.  *    Escape Key            - exit the program
  8.  *    `f' Key                - toggle filled / wireframe cylinder
  9.  *
  10.  *    Left Mousebutton, down        - start moving the eye
  11.  *    Left Mousebutton, up        - stop moving the eye
  12.  *    Mouse pointer location        - position eye
  13.  *    Middle Mousebutton, down    - zoom in
  14.  *    Right Mousebutton, down        - zoom out
  15.  */
  16. #include <GL/gl.h>
  17. #include <GL/glu.h>
  18. #include <GL/glut.h>
  19.  
  20. #include <math.h>    /* for fmodf */
  21. #include <stdio.h>    /* for printf */
  22. #include <stdlib.h>    /* for printf */
  23.  
  24. /*  Function Prototypes  */
  25.  
  26. GLvoid  initgfx( GLvoid );
  27. GLvoid  drawScene( GLvoid );
  28. GLvoid  reshape( GLsizei, GLsizei );
  29. GLvoid  keyboard( GLubyte, GLint, GLint );
  30. GLvoid  mouse( GLint, GLint, GLint, GLint );
  31. GLvoid  motion( GLint, GLint );
  32.  
  33. void resetView( GLvoid );
  34. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  35. void printHelp( char * );
  36.  
  37. /* Global Definitions */
  38.  
  39. #define KEY_ESC    27    /* ascii value for the escape key */
  40.  
  41. /* Global Variables */
  42.  
  43. enum { FRONT_VIEW, RIGHT_VIEW, TOP_VIEW, POLAR_VIEW };
  44.  
  45. static char     *windowTitles[] = {
  46.             "front view", "right view", "top view", "polar view" };
  47.  
  48. static GLint        windowIDs[POLAR_VIEW + 1];
  49.  
  50. static GLint        eventWindow;
  51.  
  52. static GLUquadricObj     *quadObj;
  53.  
  54. static GLboolean    filledFlag = GL_FALSE;
  55.  
  56. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  57. static GLint        action;
  58.  
  59. static GLdouble        xStart = 0.0, yStart = 0.0;
  60.  
  61. static GLfloat         distance, twistAngle, incAngle, azimAngle;
  62.  
  63. void
  64. main( int argc, char *argv[] )
  65. {
  66.     GLsizei width, height, windowHeight, windowWidth;
  67.     GLint windowID, i;
  68.     GLint xOrigin, yOrigin;
  69.     GLint borderSize = 8;
  70.     GLint titleHeight = 24;
  71.  
  72.     glutInit( &argc, argv );
  73.  
  74.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  75.     height = glutGet( GLUT_SCREEN_HEIGHT );
  76.     windowWidth = width / 2 - 2 * borderSize;
  77.     windowHeight = height / 2 - 2 * borderSize - titleHeight;
  78.  
  79.     for ( i = 0; i < 4; i++ ) {
  80.         /* works for up to four windows, places windows like this:
  81.          *    0   1
  82.          *    2   3
  83.          */
  84.         xOrigin = (i % 2)*width / 2 + borderSize; 
  85.         yOrigin = (i > 1)*(height / 2 + borderSize) + titleHeight;
  86.         glutInitWindowPosition( xOrigin, yOrigin );
  87.         glutInitWindowSize( windowWidth, windowHeight );
  88.         glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  89.         windowIDs[i] = glutCreateWindow(windowTitles[i]);
  90.  
  91.         initgfx();
  92.  
  93.         glutMouseFunc( mouse );
  94.         glutMotionFunc( motion );
  95.         glutKeyboardFunc( keyboard );
  96.         glutReshapeFunc( reshape );
  97.         glutDisplayFunc( drawScene ); 
  98.     }
  99.  
  100.         quadObj = gluNewQuadric ();
  101.  
  102.     printHelp( argv[0] );
  103.  
  104.     glutMainLoop();
  105. }
  106.  
  107. void
  108. printHelp( char *progname )
  109. {
  110.     fprintf(stdout, "\n%s - demonstrate how to create multiple windows\n" 
  111.     "Left Mousebutton    - move eye position in polarview window\n"
  112.     "Middle Mousebutton    - change twist angle in polarview window\n"
  113.     "Right Mousebutton    - move up/down to zoom in/out in all windows\n"
  114.     "<f> Key        - toggle wireframe/solid cylinder\n"
  115.     "<R> Key        - reset viewpoint\n"
  116.     "Escape Key        - exit the program\n\n",
  117.     progname);
  118. }
  119.  
  120. GLvoid
  121. initgfx( GLvoid )
  122. {
  123.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  124.     glShadeModel( GL_FLAT );
  125.     glEnable( GL_DEPTH_TEST );
  126.  
  127.     resetView();
  128. }
  129.  
  130. GLvoid
  131. reshape( GLsizei width, GLsizei height )
  132. {
  133.     GLdouble            aspect;
  134.  
  135.     glViewport( 0, 0, width, height );
  136.  
  137.     aspect = (GLdouble) width / (GLdouble) height;
  138.  
  139.     glMatrixMode( GL_PROJECTION );
  140.     glLoadIdentity();
  141.     gluPerspective( 45.0, aspect, 3.0, 7.0 );
  142.     glMatrixMode( GL_MODELVIEW );
  143. }
  144.  
  145. GLvoid
  146. redisplayAllWindows( GLvoid )
  147. {
  148.     int i;
  149.  
  150.     /* update all windows views */
  151.     for ( i = 0; i < 4; i++ ) {
  152.         glutSetWindow( windowIDs[i] );
  153.         glutPostRedisplay();
  154.     }
  155. }
  156.  
  157. GLvoid 
  158. keyboard( GLubyte key, GLint x, GLint y )
  159. {
  160.     switch (key) {
  161.     case 'f':    /* toggle filled mode */
  162.         filledFlag = !filledFlag;
  163.         redisplayAllWindows();
  164.         break;
  165.     case 'R':
  166.         resetView();
  167.         glutPostRedisplay();
  168.         break;
  169.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  170.         exit(0);
  171.     }
  172. }
  173.  
  174. GLvoid 
  175. mouse( GLint button, GLint state, GLint x, GLint y )
  176. {
  177.     if (state == GLUT_DOWN) {
  178.         switch (button) {
  179.         case GLUT_LEFT_BUTTON:
  180.             action = MOVE_EYE;
  181.             break;
  182.         case GLUT_MIDDLE_BUTTON:
  183.             action = TWIST_EYE;
  184.             break;
  185.         case GLUT_RIGHT_BUTTON:
  186.             action = ZOOM;
  187.             break;
  188.         }
  189.  
  190.         /* Update the saved mouse position */
  191.         xStart = x;
  192.         yStart = y;
  193.     } else {
  194.         action = MOVE_NONE;
  195.     }
  196.  
  197. }
  198.  
  199. GLvoid
  200. motion( GLint x, GLint y )
  201. {
  202.     switch (action) {
  203.     case MOVE_EYE:
  204.         /* Adjust the eye position based on the mouse position */
  205.         azimAngle += (GLdouble) (x - xStart);
  206.         incAngle -= (GLdouble) (y - yStart);
  207.         glutPostRedisplay();
  208.         break;
  209.     case TWIST_EYE:
  210.         /* Adjust the eye twist based on the mouse position */
  211.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  212.         glutPostRedisplay();
  213.         break;
  214.     case ZOOM:
  215.         /* Adjust the eye distance based on the mouse position */
  216.         distance -= (GLdouble) (y - yStart)/10.0;
  217.         redisplayAllWindows();
  218.         break;
  219.     default:
  220.         printf("unknown action %d\n", action);
  221.     }
  222.     
  223.     /* Update the stored mouse position for later use */
  224.     xStart = x;
  225.     yStart = y;
  226. }
  227.  
  228. void
  229. resetView( GLvoid )
  230. {
  231.     distance = 4.5;
  232.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  233.     incAngle = 0.0;
  234.     azimAngle = 0.0;
  235. }
  236.  
  237. void
  238. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  239.             GLfloat twist)
  240. {
  241.     glTranslatef( 0.0, 0.0, -distance);
  242.     glRotatef( -twist, 0.0, 0.0, 1.0);
  243.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  244.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  245. }
  246.  
  247. GLvoid
  248. drawGrid( GLvoid )
  249. {
  250.     GLfloat     x, y;
  251.  
  252.     glBegin( GL_LINES );
  253.     for ( x = -1.0; x < 1.1; x += 0.1 ) {
  254.         glVertex2f( x, -1.0 );
  255.         glVertex2f( x, 1.0 );
  256.     }
  257.     for ( y = -1.0; y < 1.1; y += 0.1 ) {
  258.         glVertex2f( -1.0, y );
  259.         glVertex2f( 1.0, y );
  260.     }
  261.     glEnd();
  262. }
  263.  
  264. GLvoid
  265. drawScene( GLvoid )
  266. {
  267.     GLint windowID = glutGetWindow();
  268.  
  269.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  270.     static GLfloat    redColor[] = { 1.0, 0.0, 0.0 };
  271.     static GLfloat    darkGreenColor[] = { 0.0, 0.5, 0.0 };
  272.  
  273.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  274.  
  275.     glPushMatrix();
  276.         if ( windowID == windowIDs[FRONT_VIEW] )
  277.             polarView( distance, 0, 90, 0 );
  278.         else if ( windowID == windowIDs[RIGHT_VIEW] )
  279.             polarView( distance, 0, 90, -90 );
  280.         else if ( windowID == windowIDs[TOP_VIEW] )
  281.             polarView( distance, 0, 0, 0 );
  282.         else if ( windowID == windowIDs[POLAR_VIEW] )
  283.             polarView( distance, azimAngle, incAngle, twistAngle );
  284.  
  285.         glColor3fv( redColor );
  286.         glutSolidCone( 0.3, 0.6, 15, 15 );
  287.  
  288.         glColor3fv( darkGreenColor );
  289.         glPushMatrix();
  290.             glTranslatef( 0.8, -0.65, 0.0 );
  291.             glRotatef( 30.0, 1.0, 0.5, 1.0 );
  292.             glRotatef( 90.0, 1.0, 0.0, 0.0 );
  293.             glTranslatef( 0.0, -0.0, -0.3 );
  294.             if ( filledFlag == GL_TRUE )
  295.                 gluQuadricDrawStyle(quadObj, GLU_FILL);
  296.             else
  297.                 gluQuadricDrawStyle(quadObj, GLU_LINE);
  298.             gluCylinder(quadObj, 0.3, 0.3, 0.6, 12, 2);
  299.         glPopMatrix();
  300.  
  301.         glColor3fv( whiteColor );
  302.         drawGrid();
  303.  
  304.     glPopMatrix();
  305.     glutSwapBuffers();
  306.  
  307. }
  308.